package com.ruoyi.edu.controller.front;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.constant.CourseStatusConstants;
import com.ruoyi.common.core.constant.DeletedConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.edu.api.domain.EduCourse;
import com.ruoyi.edu.api.domain.EduTeacher;
import com.ruoyi.edu.service.IEduCourseService;
import com.ruoyi.edu.service.IEduTeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 前台教师模块
 * @author isahino
 * @version 1.0
 * @date 2023/9/22 23:57
 */
@RestController
@RequestMapping("/front_teacher")
public class FrontEduTeacherController {

    @Autowired
    private IEduTeacherService eduTeacherService;
    @Autowired
    private IEduCourseService eduCourseService;

    /**
     * 查询讲师列表
     */
    @GetMapping("/list/{page}/{limit}")
    public AjaxResult list(@PathVariable Integer page, @PathVariable Integer limit) {
        Page<EduTeacher> result = new Page<>(page, limit);
        LambdaQueryWrapper<EduTeacher> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(EduTeacher::getSort);
        queryWrapper.eq(EduTeacher::getIsDeleted, DeletedConstants.UN_DELETED);
        eduTeacherService.page(result, queryWrapper);
        return AjaxResult.success(result);
    }

    /**
     * 查询讲师详情
     */
    @GetMapping("/info/{id}")
    public AjaxResult getTeacherFrontInfo(@PathVariable String id){
        //1.根据讲师id查询讲师基本信息
        EduTeacher eduTeacher = eduTeacherService.getById(id);
        //2.根据讲师id查询讲师的所有课程
        LambdaQueryWrapper<EduCourse> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EduCourse::getTeacherId, id);
        //需要是上架且发布了的课程
        wrapper.eq(EduCourse::getIsDeleted, DeletedConstants.UN_DELETED);
        wrapper.eq(EduCourse::getStatus, CourseStatusConstants.RELEASED);
        wrapper.orderByDesc(EduCourse::getViewCount);
        List<EduCourse> courseList = eduCourseService.list(wrapper);
        Map<String, Object> result = new HashMap<>(2);
        result.put("teacher", eduTeacher);
        result.put("courseList", courseList);
        return AjaxResult.success(result);
    }

}
